package com.touchealth.test.classload;

import org.apache.commons.lang3.text.WordUtils;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * @author zhangyanqiang
 * @date 2021/1/8
 **/
public class LoadSameNameClass {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, MalformedURLException {

        System.out.println( WordUtils.swapCase("ABCdfsdsd"));

        File file = new File("/Users/zhangyanqiang/.m2/repository_ecommerce/commons-lang/commons-lang/2.6/commons-lang-2.6.jar");

        URL urls = file.toURL();

        URLClassLoader customClassLoader = new URLClassLoader(new URL[]{urls},null);

        Class cl = customClassLoader.loadClass("org.apache.commons.lang.WordUtils");
        System.out.println(customClassLoader.getURLs()[0]);

        Method method = cl.getDeclaredMethod("swapCase",String.class);

        System.out.println(method.invoke(cl,"ABCdfsdsd"));

        ClassLoader classLoader = LoadSameNameClass.class.getClassLoader();
        while (classLoader != null){
            System.out.println(classLoader.toString());
            classLoader = classLoader.getParent();

        }
    }
}
